home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / T U R B O Language / Turbo Pascal V7.0 / TVFM.ZIP / VIEWHEX.PAS < prev    next >
Encoding:
Pascal/Delphi Source File  |  1992-10-30  |  5.0 KB  |  216 lines

  1. {************************************************}
  2. {                                                }
  3. {   Turbo Vision File Manager Demo               }
  4. {   Copyright (c) 1992 by Borland International  }
  5. {                                                }
  6. {************************************************}
  7.  
  8. {$X+}
  9.  
  10. unit ViewHex;
  11.  
  12. interface
  13.  
  14. uses Drivers, Objects, Memory, Views;
  15.  
  16. type
  17.  
  18.   PHexViewer = ^THexViewer;
  19.   THexViewer = object(TScroller)
  20.     FileBuf: Pointer;
  21.     BufSize: Word;
  22.     MaxLines: Integer;
  23.     constructor Init(var Bounds:TRect; AVScrollBar: PScrollBar;
  24.       const Name: FNameStr);
  25.     destructor Done; virtual;
  26.     procedure Draw; virtual;
  27.   end;
  28.  
  29.   PHexWindow = ^THexWindow;
  30.   THexWindow = object(TWindow)
  31.     Interior: PHexViewer;
  32.     constructor Init(var Bounds: TRect; Filename: FNameStr);
  33.     destructor Done; virtual;
  34.     procedure SizeLimits(var Min, Max: TPoint); virtual;
  35.     function GetPalette: PPalette; virtual;
  36.   end;
  37.  
  38. implementation
  39.  
  40. uses MsgBox;
  41.  
  42. type
  43.   String2 = String[2];
  44.  
  45.   PByteBuffer = ^TByteBuffer;
  46.   TByteBuffer = array[0..$FFFE] of Byte;
  47.  
  48.  
  49. { Store hex characters directly into the string location pointed to by P }
  50. { No bounds checking done! }
  51. procedure AddHexByte(B: Byte; P: Pointer);
  52. const
  53.   HexChars : array[0..15] of char = '0123456789ABCDEF';
  54. type
  55.   P2Char = ^T2Char;
  56.   T2Char = array[0..1] of Char;
  57. begin
  58.   P2Char(P)^[0] := HexChars[ (B and $F0) shr 4 ];
  59.   P2Char(P)^[1] := HexChars[ B and $0F ];
  60. end;
  61.  
  62.  
  63. { THexViewer }
  64.  
  65. constructor THexViewer.Init(var Bounds:TRect; AVScrollBar: PScrollBar;
  66.   const Name: FNameStr);
  67. var
  68.   F: File;
  69.   Result: Word;
  70.   FSize : Longint;
  71.   Msg: String;
  72.   SaveMode : Byte;
  73. begin
  74.   inherited Init(Bounds, nil, AVScrollBar);
  75.   GrowMode := gfGrowHiX + gfGrowHiY;
  76.   Options := Options or ofTileable;
  77.   FileBuf := nil;
  78.   BufSize := 0;
  79.   Msg := '';     { no errors encountered   }
  80.  
  81.   { open the file }
  82.  
  83.   SaveMode := FileMode;
  84.   FileMode := 0;        { make sure we open as Read-Only }
  85.   Assign(F, Name);
  86.   {$I-}
  87.   Reset(F,1);
  88.   {$I+}
  89.   FileMode := SaveMode;
  90.   Result := IOResult;
  91.   if Result = 0 then
  92.   begin
  93.     FSize := FileSize(F);
  94.  
  95.     if FSize > $FFFE then
  96.     begin
  97.       FSize := $FFFE;
  98.       Msg := 'File is larger than 64k.  Display will be truncated';
  99.     end;
  100.  
  101.     if FSize > MaxAvail - LowMemSize then  { use standard safety size }
  102.     begin
  103.       FSize := MaxAvail - LowMemSize;
  104.       if FSize > 0 then
  105.         Msg := 'File too large for available memory.  Display will be truncated.'
  106.       else Msg := 'Not enough memory for safety pool!';
  107.     end;
  108.  
  109.     if FSize > 0 then
  110.     begin
  111.       GetMem(FileBuf, FSize);
  112.       BlockRead(F, FileBuf^, FSize, Result);
  113.       BufSize := FSize;
  114.     end;
  115.  
  116.     Close(F);
  117.   end
  118.   else Msg := 'Unable to open this file!';
  119.  
  120.   { Display any message that was generated }
  121.   if Msg <> '' then
  122.     MessageBox(Msg, nil, mfInformation+mfOKButton);
  123.  
  124.   MaxLines := BufSize div 16;
  125.   if BufSize mod 16 > 0 then Inc(MaxLines);
  126.   SetLimit(0, MaxLines);
  127. end;
  128.  
  129. destructor THexViewer.Done;
  130. begin
  131.   if (BufSize > 0) and (FileBuf <> nil) then FreeMem(FileBuf, BufSize);
  132.   inherited Done;
  133. end;
  134.  
  135. procedure THexViewer.Draw;
  136. const
  137.   VWidth = 69;      { total width of view }
  138.   HStart = 7;       { starting column of hex dump }
  139.   CStart = 56;      { starting column of character dump }
  140.   LineChar = #179;  { vertical line character }
  141. var
  142.   B: TDrawBuffer;
  143.   S: String;
  144.   C: Word;
  145.   Offset: Word;
  146.   x,y : Byte;
  147.   i,byt: Byte;
  148.   L: Longint;
  149. begin
  150.   C := GetColor(1);
  151.   for y := 0 to Size.Y-1 do
  152.   begin
  153.     FillChar(S[1], VWidth, 32);
  154.     S[0] := Char(VWidth);
  155.     MoveChar(B, #32, C, Size.X);
  156.     Offset := (Delta.Y + Y) * 16;
  157.     if (Delta.Y + Y) < MaxLines then
  158.     begin
  159.       L := (Delta.Y + Y) * 16;
  160.       FormatStr(S, '%04x:', L);
  161.       S[0] := Char(VWidth);
  162.       i := HStart;
  163.       for x := 0 to 15 do
  164.       begin
  165.         if Offset + x < BufSize then
  166.         begin
  167.           byt := PByteBuffer(FileBuf)^[Offset+x];
  168.           AddHexByte(byt, @S[i]);
  169.           S[CStart + x] := Char(byt);
  170.           Inc(i,3);
  171.         end;
  172.       end;
  173.     end;
  174.     S[CStart - 1] := LineChar;
  175.     MoveStr(B, S, C);
  176.     WriteLine(0,Y,Size.X,1,B);
  177.   end;
  178. end;
  179.  
  180.  
  181. { THexWindow }
  182. constructor THexWindow.Init(var Bounds: TRect; Filename: FNameStr);
  183. var
  184.   R: TRect;
  185.   SB: PScrollBar;
  186. begin
  187.   inherited Init(Bounds, Filename, wnNoNumber);
  188.   GetExtent(R);
  189.   SB := StandardScrollBar(sbVertical + sbHandleKeyboard);
  190.   Insert(SB);
  191.   R.Grow(-1,-1);
  192.   Interior := New(PHexViewer, Init(R, SB, Filename));
  193.   Insert(Interior);
  194. end;
  195.  
  196. destructor THexWindow.Done;
  197. begin
  198.   if Interior <> nil then Dispose(Interior, Done);
  199.   inherited Done;
  200. end;
  201.  
  202. procedure THexWindow.SizeLimits(var Min, Max: TPoint);
  203. begin
  204.   inherited SizeLimits(Min, Max);
  205.   Max.X := 72;
  206. end;
  207.  
  208. function THexWindow.GetPalette: PPalette;
  209. const
  210.   MyPal : String[Length(CGrayWindow)] = CCyanWindow;
  211. begin
  212.   GetPalette := @MyPal;
  213. end;
  214.  
  215. end. { unit }
  216.